package com.quyeying.utils.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quyeying.entity.TblOrderBaseEntity;
import com.quyeying.entity.TblOrderStatusRecordEntity;
import com.quyeying.service.TblOrderBaseService;
import com.quyeying.service.TblOrderStatusRecordService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName OrderTask
 * @Description 处理超时订单
 * @Author Jll
 * @Date 2022/8/18 10:04
 * @Version 1.0
 */
@Component("orderTask")
public class OrderTask {

    @Resource
    private TblOrderBaseService tblOrderBaseService;

    @Resource
    private TblOrderStatusRecordService tblOrderStatusRecordService;

    public void cancelOrder() {
        QueryWrapper<TblOrderBaseEntity> orderBaseEntityQueryWrapper = new QueryWrapper<>();
        orderBaseEntityQueryWrapper.eq("pay_status", 0)
                .eq("order_status", 0).apply("create_time < DATE_SUB(NOW(),INTERVAL 60 MINUTE) ");
        List<TblOrderBaseEntity> list = tblOrderBaseService.list(orderBaseEntityQueryWrapper);
        TblOrderStatusRecordEntity orderStatusRecordEntity = new TblOrderStatusRecordEntity();
        list.stream().forEach(item->{
            item.setOrderStatus(4);
            //在订单记录表插入数据
            orderStatusRecordEntity.setOrderId(item.getId());
            orderStatusRecordEntity.setOrderStatus(4);
            tblOrderStatusRecordService.add(orderStatusRecordEntity);
        });
        tblOrderBaseService.saveOrUpdateBatch(list);
    }
}
